package com.zjz.demo.Pool;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/10/09/10:08
 * @Description: 测试最大线程数:Max+Queue,学习线程池运行线程
 * @Target: 自建pool,测试
 */
public class BuildPool {
    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy()// 不处理,抛出异常
//                new ThreadPoolExecutor.CallerRunsPolicy() // 哪来的去哪里,目前是main执行
//                new ThreadPoolExecutor.DiscardPolicy() // 队列满了就丢掉,不会抛出异常
                new ThreadPoolExecutor.DiscardOldestPolicy() // 队列满了就和最早的去竞争,不会抛出异常
        );

        try {
            // 最大承载:Queue+max
            // 超出最大承载:
            for (int i = 0; i < 15; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"执行");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}
